package com.snowy.hidden.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fhs.trans.service.impl.DictionaryTransService;
import com.snowy.hidden.biz.domain.model.Dict;
import com.snowy.hidden.biz.mapper.DictMapper;
import com.snowy.hidden.biz.service.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 字典(Dict)表服务实现类
 *
 * @author makejava
 * @since 2023-12-29 23:04:18
 */
@Service("dictService")
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

	@Resource
    private DictMapper dictMapper;

	@Resource
	private DictionaryTransService dictionaryTransService;

	private static final String ROOT_PARENT_ID = "0";


	@Override
	public List<Dict> getAllDictByRootDictValue(String parentDictValue) {
		List<Dict> bizDictList = this.baseMapper.listBizDictByParentDictValue(parentDictValue);
		return bizDictList;
	}

	@Override
	public String getChildeLableByValue(String parentDictValue, String childedictValue) {
		List<Dict> bizDictList = this.baseMapper.listBizDictByParentDictValue(parentDictValue);
		// 根据value获取label
		 for (Dict dict : bizDictList) {
			 if(dict.getDictValue().equals(childedictValue)){
				 return dict.getDictLabel();
			 }
		 }
		return "未知";
	}

//	@Override
//	public void afterPropertiesSet() throws Exception {
//		refreshTransCache();
//	}
//
//	private void refreshTransCache() {
//		// 异步不阻塞主线程，不会 增加启动用时
//		CompletableFuture.supplyAsync(() -> {
//			// 使用redis能解决共享问题，但是性能没有直接取缓存的好。
//			dictionaryTransService.makeUseRedis();
//			List<Dict> devDictList = super.list(new LambdaQueryWrapper<>());
//			// 非root级别的字典根据ParentId分组
//			Map<String,List<Dict>> devDictGroupByPIDMap = devDictList.stream().filter(dict -> !ROOT_PARENT_ID
//					.equals(dict.getParentId())).collect(Collectors.groupingBy(Dict::getParentId));
//			Map<String,String> parentDictIdValMap = devDictList.stream().filter(dict -> ROOT_PARENT_ID
//					.equals(dict.getParentId())).collect(Collectors.toMap(Dict::getId, Dict::getDictValue));
//			for (String parentId : parentDictIdValMap.keySet()) {
//				if(devDictGroupByPIDMap.containsKey(parentId)){
//					dictionaryTransService.refreshCache(parentDictIdValMap.get(parentId), devDictGroupByPIDMap.get(parentId).stream()
//							.collect(Collectors.toMap(Dict::getDictValue, Dict::getDictLabel)));
//				}
//			}
//			return null;
//		});
//	}
}
